<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			set_time_limit(0);
			parent::__construct();
		}
		//开始自动程序
		public function run(){
		$sql="select * from yang_currency where is_line = 1";
		$currency_list=$this->select($sql);
			foreach ($currency_list as $k =>$v){
				//获取货物当前价格
				$random=$this->random();
				$now_price=$this->result($v['currency_url'],$v['lever'])+$random;
				//获取该货物所有未平仓的点位订单
				$sql1="select * from yang_point_orders where status = 0 and currency_id =".$v['currency_id'];
				$point_orders=$this->select($sql1);
				if(!empty($point_orders)){
					foreach($point_orders as $kk =>$vv){
						//获取订单利率
						$ratesql="select * from yang_point_config where id =".$vv['point_type'];
						$r=$this->find($ratesql);
						$rate=$r['rate'];
						$point=$r['point'];
						//买涨订单平仓
						if($vv['type']==1){
							if($now_price>=$vv['buy_point']+$point){
							//计算获利金钱
							$money=$vv['money']*(1+$rate/100);
							$money2=$vv['money']*$rate/100;							
							//给用户加获利金钱
							$profitsql="update yang_member set rmb = rmb +".$money." where member_id = ".$vv['member_id'];
							$this->query($profitsql);
							//修改点位订单状态
							$statussql="update yang_point_orders set status = 1 , profit = ".$money2." trade_time =".time()." where id= ".$vv['id'];
							$this->query($statussql);
							} 
							if($now_price<=$vv['buy_point']-$point){
							//修改点位订单状态
							$statussql="update yang_point_orders set status = 1 ,profit = 0 ,trade_time =".time()." where id= ".$vv['id'];
							$this->query($statussql);
							}							
						}
						//买跌订单平仓
						if($vv['type']==2){
							if($now_price<=$vv['buy_point']-$point){
							//计算获利金钱
							$money=$vv['money']*(1+$rate/100);
							//给用户加获利金钱						
							$profitsql="update yang_member set rmb = rmb +".$money." where member_id = ".$vv['member_id'];
							$this->query($profitsql);
							//修改点位订单状态
							$statussql="update yang_point_orders set status = 1 ,trade_time =".time()." where id= ".$vv['id'];
							$this->query($statussql);
							}
							if($now_price>=$vv['buy_point']+$point){
								echo 2;
							//修改点位订单状态
							$statussql="update yang_point_orders set status = 1 ,trade_time =".time()." where id= ".$vv['id'];
							$this->query($statussql);
							}
							
						}
					}
				}
			}
		}
		private function random(){
		$time=time();
		$timen=substr($time, -2);
		$minute=date('i',$time);
		$second=date('s',$time);
		$num=round(($timen+$minute+$second)/300,2);
		if($timen%2==0){
			$num=0-$num;
		}
		return $num;
	}
		private function result($code,$lever){
		
		//$url = "http://data.ronmei.com:8888/Price?code=CL1712";
		$url = "http://data.ronmei.com:8888/Price?code=".$code;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result = curl_exec($ch);
		curl_close ($ch);
		$newresult=json_decode($result);
		return $newresult->Price*$lever;
	}
		
	}
	$auto=new auto();
	$auto->run();
	
?>